package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.MeMoralEducationRecordJdbc;
import com.wuseguang.springcloud.dao.repository.MeMoralEducationRecordRepository;
import com.wuseguang.springcloud.entity.MeMoralEducationRecord;
import com.wuseguang.springcloud.service.MeMoralEducationRecordService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 德育-班级德育记录 服务实现类
 * </p>
 *
 * @author
 * @since 2020-06-28
 */
@Service
public class MeMoralEducationRecordServiceImpl implements MeMoralEducationRecordService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MeMoralEducationRecordRepository meMoralEducationRecordRepository;
    @Autowired
    private MeMoralEducationRecordJdbc meMoralEducationRecordJdbc;


    @Override
    public Result findMoralEducationRecord(String deviceid) {
        logger.info("查询本周班级德育得分");
        //总分
        Integer totalScore = 0;
        Map<String, Object> stringObjectMap4 = meMoralEducationRecordJdbc.queryDevice(deviceid);
        //查询德育项
        String sql = "select id, `name`,score from me_moral_education where school_id = ?";
        List<Map<String, Object>> list = meMoralEducationRecordJdbc.queryListMap(sql, stringObjectMap4.get("school_id"));
        for (Map<String, Object> map : list) {
            logger.info("查询"+map.get("name")+"项本周的扣分总和");
            sql = "select IFNULL(sum(demerit),0) count from me_moral_education_record " +
                    "where moral_education_id = ? and class_id = ? and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now())";
            //查询这个班这个项目本周扣分总和
            Integer sum = meMoralEducationRecordJdbc.queryCount(sql,map.get("id"),stringObjectMap4.get("classid"));
            //评价项总分
            Integer zong = Integer.valueOf(map.get("score").toString());
            //分数
            Integer score = zong+sum;
            //添加
            totalScore+=score;
            map.put("score",score);
            map.put("totalScore",zong);

        }

        logger.info("查询今天扣分");
        sql ="select IFNULL(sum(demerit),0) count from me_moral_education_record where  class_id = ? and to_days(date_format(add_time,'%Y-%m-%d')) = to_days(now())";
        Integer koufen = meMoralEducationRecordJdbc.queryCount(sql,stringObjectMap4.get("classid"));

        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("list",list);
        returnMap.put("koufen",koufen);
        //平均分
        returnMap.put("avg",totalScore);
        return new Result(returnMap);
    }

    @Override
    public Result findRankingLastWeek(String deviceid) {

        Map<String, Object> stringObjectMap4 = meMoralEducationRecordJdbc.queryDevice(deviceid);

        //查询学校总分
        String sql = "select IFNULL(sum(score),0) count from me_moral_education where school_id = ?";
        //德育项总分
        Integer score = meMoralEducationRecordJdbc.queryCount(sql,stringObjectMap4.get("school_id"));
        //查询每个班上周的德育分数
        sql = "select c.`name` ,("+score+"-IFNULL(sum(demerit) ,0)) demerit from   classes c " +
                " left join me_moral_education_record mer on(c.id=mer.class_id and YEARWEEK(date_format(add_time,'%Y-%m-%d')) = YEARWEEK(now())-1) " +
                " left join grade g on(c.grade_id = g.id ) where c.school_id = ? and g.id=?  GROUP BY c.id ORDER BY ("+score+"-IFNULL(sum(demerit) ,0)) desc";

        List<Map<String,Object>> list = meMoralEducationRecordJdbc.queryListMap(sql,stringObjectMap4.get("school_id"),stringObjectMap4.get("grade_id"));
        return new Result(list);
    }
}
